﻿using System.Collections.Generic;
using Dto;
using SqlServerDAL;

namespace Business
{
    public class ManageMenuBusiness
    {
        private readonly ManageMenuDAL manageMenuDal = new ManageMenuDAL();

        #region GetMenuBySuperiorID[获取显示的菜单数据]

        /// <summary>
        /// 获取显示的菜单数据
        /// </summary>
        /// <param name="superiorId">上级ID</param>
        /// <param name="getChild">是否加载下级ID</param>
        /// <param name="roleMenuKeys"></param>
        /// <returns></returns>
        public List<ManageMenuDto> GetMenuBySuperiorId(int superiorId, bool getChild, ICollection<string> roleMenuKeys)
        {
            List<ManageMenuDto> dtoList = null;
            var hts = manageMenuDal.GetList(superiorId);
            if (hts != null && hts.Count > 0)
            {
                dtoList = new List<ManageMenuDto>();
                foreach (var ht in hts)
                {
                    var manageMenudto = FillDto.FillManageMenudto(ht);
                    if (roleMenuKeys == null || !roleMenuKeys.Contains(manageMenudto.MenuKey)) continue;
                    if (getChild)
                    {
                        manageMenudto.ChildMenuList = new List<ManageMenuDto>();
                        var childHts = manageMenuDal.GetList(manageMenudto.MenuID);
                        foreach (var childHt in childHts)
                        {
                            var childDto = FillDto.FillManageMenudto(childHt);
                            if (roleMenuKeys.Contains(childDto.MenuKey))
                            {
                                manageMenudto.ChildMenuList.Add(childDto);
                            }
                        }
                    }
                    dtoList.Add(manageMenudto);
                }
            }
            return dtoList;
        }

        #endregion

        #region GetFirstMenu[通过上级ID获取列表中的第一个菜单]

        /// <summary>
        /// 通过上级ID获取列表中的第一个菜单
        /// </summary>
        /// <param name="superiorId">上级ID</param>
        /// <returns>取得的菜单ID</returns>
        public int GetFirstMenu(int superiorId)
        {
            var ht = manageMenuDal.FindFirstBySuperiorID(superiorId);
            var dto = FillDto.FillManageMenudto(ht);
            return dto != null ? dto.MenuID : 0;
        }

        #endregion

        #region GetMenuById[通过ID获取菜单内容]

        /// <summary>
        /// 通过ID获取菜单内容
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ManageMenuDto GetMenuById(int id)
        {
            ManageMenuDto dto = null;
            var ht = manageMenuDal.FindById(id);
            if (ht != null)
            {
                dto = FillDto.FillManageMenudto(ht);
            }
            return dto;
        }

        #endregion

        #region GetMenuTrick[获取菜单层级轨迹]

        /// <summary>
        /// 获取菜单层级轨迹
        /// </summary>
        /// <param name="superiorId">上一级菜单ID</param>
        /// <returns></returns>
        public ManageMenuDto GetSuperiorMenu(int superiorId)
        {
            ManageMenuDto dto = null;
            var entity = manageMenuDal.FindFirstBySuperiorID(superiorId);
            if (entity != null)
            {
                dto = FillDto.FillManageMenudto(entity);
            }
            return dto;
        }

        #endregion

        public bool SaveMenu(ManageMenuDto dto)
        {
            var b = manageMenuDal.Save(dto);
            return b;
        }

        public bool DeleteMenu(int id)
        {
            var b = manageMenuDal.Delete(id);
            return b;
        }
    }
}